﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Zhuang.WinForm.AppCode.Common;
using Zhuang.Utility.Common.DataBase;

namespace Zhuang.WinForm.AuthorityManager
{
    public partial class UserAuthorithSetting : Form
    {
        public UserAuthorithSetting()
        {
            InitializeComponent();
        }


        private void UserAuthorithSetting_Load(object sender, EventArgs e)
        {
            doSearch();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            doSearch();
        }

        private void doSearch()
        {
            string condiction = "";
            string strSql = "";

            condiction = tbCondiction.Text.Trim();
            strSql = "select userid,username From App_User where username like '%"+condiction+"%'";

            try
            {
                dgvUser.DataSource= SqlHelper.Query(strSql).Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void dgvUser_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                
                searchMenu(dgvUser.Rows[e.RowIndex].Cells["userid"].Value.ToString().Trim());
            }

        }

        private void searchMenu(string userid)
        {
            string strSql = "select a.menuid,a.menuname from App_Menu a,App_UserToMenu b where a.menuid=b.menuid and b.userid="+userid;
            try
            {
                dgvMenu.DataSource= SqlHelper.Query(strSql).Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvUser.SelectedRows.Count < 1)
            {
                MessageBox.Show("请选择用户！");
                return;
            }

            string strUserId = dgvUser.SelectedRows[0].Cells["userid"].Value.ToString().Trim();

            SelectMenu sm = new SelectMenu(strUserId);
            if (sm.ShowDialog() == DialogResult.OK)
            {
                searchMenu(strUserId);
            }

        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvUser.SelectedRows.Count < 1)
            {
                MessageBox.Show("请选择用户！");
                return;
            }
            else if (dgvMenu.SelectedRows.Count < 1)
            {
                MessageBox.Show("请选择要删除菜单！");
                return;
            }


            string strUserId = dgvUser.SelectedRows[0].Cells["userid"].Value.ToString().Trim();

            try
            {

                foreach (DataGridViewRow dgvr in dgvMenu.SelectedRows)
                {
                    string strMenuId = dgvr.Cells["menuid"].Value.ToString().Trim();

                    string strSql = "delete App_UserToMenu where userid=" + strUserId + " and menuid= " + strMenuId;


                    SqlHelper.ExecuteSql(strSql);

                }
            

            
                

                searchMenu(strUserId);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }


    }
}
